//
//  BaseController.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/21.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit

class BaseController: UIViewController {
    
//    var userLogin = true
    var visitorInfoDic: [String : String]?
    
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    lazy var navItem = UINavigationItem()
    var tableView: UITableView?
    var refreshControl: BORefreshControl?
    var isPullup = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        NetworkManager.shared().userLogon ? loadData() : ()
        
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: NSNotification.Name(rawValue: UserloginSuccessNotification), object: nil)
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UserloginSuccessNotification), object: nil)
    }
    
    override var title: String?{
        didSet{
            navItem.title = title
        }
    }
    
    func loadData() {
        refreshControl?.endRefreshing()
    }
    
}

extension BaseController {

    @objc fileprivate func loginSuccess(n:Notification) {
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        view = nil
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UserloginSuccessNotification), object: nil)
    }
    
    @objc fileprivate func loginButton() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: UserShouldLoginNotification), object: nil)
    }
    
    @objc fileprivate  func registerButton() {
        print("注册")
    }
    
}

extension BaseController {
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.white
        automaticallyAdjustsScrollViewInsets = false
        setupNavigationBar()
        
        (NetworkManager.shared().userLogon) ? setupTableView() : setupVisitorView()
        
    }
    
    func setupTableView(){
        tableView = UITableView(frame: view.bounds, style: .plain)
//        view.addSubview(tableView!)
        tableView?.delegate = self
        tableView?.dataSource = self
        view.insertSubview(tableView!, belowSubview: navigationBar)
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        refreshControl = BORefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    private func setupVisitorView() {
        let visitorView = VisitorView(frame: view.bounds)
        visitorView.visitorInfo = visitorInfoDic
        visitorView.loginButton.addTarget(self, action: #selector(loginButton), for: .touchUpInside)
        visitorView.registerButton.addTarget(self, action: #selector(registerButton), for: .touchUpInside)
//        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(registerButton))
//        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(loginButton))
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerButton))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginButton))
        view.insertSubview(visitorView, belowSubview: navigationBar)
    }
    
    private func setupNavigationBar() {
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
        navigationBar.items = [navItem]
        navigationBar.tintColor = UIColor.orange
        view.addSubview(navigationBar)
    }
}

extension BaseController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        if row<0 || section<0 {
            return
        }
        let count = tableView.numberOfRows(inSection: section)
        if row == (count-1) && !isPullup{
            isPullup = true
            loadData()
        }
        
    }
}
